package com.ecommerce.service;

import com.ecommerce.entity.Cart;
import com.ecommerce.entity.Product;
import com.ecommerce.repository.CartRepository;
import com.ecommerce.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CartService {
    // 注入CartRepository
    @Autowired
    private CartRepository cartRepository;
    
    // 注入ProductRepository
    @Autowired
    private ProductRepository productRepository;

    // 获取用户购物车列表（包含商品信息）
    public List<Map<String, Object>> getCartList(Long userId) {
        List<Cart> cartItems = cartRepository.findByUserId(userId);
        List<Map<String, Object>> result = new ArrayList<>();
        
        for (Cart cart : cartItems) {
            Map<String, Object> item = new HashMap<>();
            item.put("id", cart.getId());
            item.put("userId", cart.getUserId());
            item.put("productId", cart.getProductId());
            item.put("quantity", cart.getQuantity());
            item.put("checked", cart.getChecked());
            
            // 获取商品信息
            Product product = productRepository.findById(cart.getProductId()).orElse(null);
            if (product != null) {
                item.put("productName", product.getName());
                item.put("productImage", product.getMainImage());
                item.put("price", product.getPrice());
            }
            
            result.add(item);
        }
        
        return result;
    }

    // 添加商品到购物车
    public Cart addToCart(Long userId, Long productId, Integer quantity) {
        // 查询是否已存在该商品
        Cart cart = cartRepository.findByUserIdAndProductId(userId, productId);
        if (cart != null) {
            // 已存在则增加数量
            cart.setQuantity(cart.getQuantity() + quantity);
        } else {
            // 不存在则新建
            cart = new Cart();
            cart.setUserId(userId);
            cart.setProductId(productId);
            cart.setQuantity(quantity);
            cart.setChecked(true);
        }
        return cartRepository.save(cart);
    }

    // 更新购物车商品数量
    public Cart updateCart(Long userId, Long productId, Integer quantity) {
        Cart cart = cartRepository.findByUserIdAndProductId(userId, productId);
        if (cart != null) {
            cart.setQuantity(quantity);
            return cartRepository.save(cart);
        }
        return null;
    }

    // 删除购物车商品
    public void removeCartItem(Long userId, Long productId) {
        Cart cart = cartRepository.findByUserIdAndProductId(userId, productId);
        if (cart != null) {
            cartRepository.delete(cart);
        }
    }

    // 清空购物车
    @Transactional
    public void clearCart(Long userId) {
        cartRepository.deleteByUserId(userId);
    }
}